package day0401;

import java.util.*;
import java.util.stream.Collectors;

public class ShootScore3 {
    /**
     * 3.排序射击比赛成绩
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        List<Integer> ids = Arrays.stream(sc.nextLine().split(",")).map(Integer::parseInt).collect(Collectors.toList());
        List<Integer> scores = Arrays.stream(sc.nextLine().split(",")).map(Integer::parseInt).collect(Collectors.toList());
        sc.close();
        HashMap<Integer, List<Integer>> id2Scores = new HashMap<>();
        for (int i = 0; i < n; i++) {
            Integer id = ids.get(i);
            Integer score = scores.get(i);
            List<Integer> scoreList = id2Scores.getOrDefault(id, new ArrayList<>());
            scoreList.add(score);
            id2Scores.put(id, scoreList);
        }
        StringBuilder sb = new StringBuilder();
        id2Scores.entrySet().stream()
                .filter(x -> x.getValue().size() >= 3)
                .sorted((o1, o2) -> {
                    Integer sum1 = sumT3(o1.getValue());
                    Integer sum2 = sumT3(o2.getValue());
                    if (Objects.equals(sum1, sum2)) {
                        return o2.getKey() - o1.getKey();
                    }
                    return sum2 - sum1;
                })
                .map(Map.Entry::getKey)
                .forEach(x -> sb.append(x).append(","));
        System.out.println(sb.substring(0,sb.length()-1));
    }

    private static Integer sumT3(List<Integer> list) {
        Collections.sort(list);
        int sum = 0;
        for (int i = list.size() - 1; i >= list.size() - 3; i--) {
            sum += list.get(i);
        }
        return sum;
    }


}

